package com.pacifico.telebusca.web.listener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

public class AutenticacionListenerFailure implements
		AuthenticationFailureHandler {

	public void onAuthenticationFailure(HttpServletRequest arg0,
			HttpServletResponse arg1, AuthenticationException arg2)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) arg2
				.getAuthentication();
		if (usernamePasswordAuthenticationToken != null) {
			String nombreUsuario = (String) usernamePasswordAuthenticationToken
					.getPrincipal();
			System.out.println("AutenticacionListenerFailure " + nombreUsuario);
		}
		System.out.println("error" + arg2.getMessage());
		//String param1After = URLEncoder.encode(param1Before, "UTF-8");
		arg1.sendRedirect(arg0.getContextPath() + "/acceso.jsp?error=1&mensaje="+arg2.getMessage());

	}

}
